#!/bin/sh

. scripts/functions

# Define the Name of the test as "TestName".
# Should be the same as the filename, therefore we use the filename as default.
TestName="`basename $0`"

# set other test specific variables
Client=susan-fd
JobName=backup-susan-fd
when=`perl -Mscripts::functions -e "get_time(300)"`

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Config files will be copied to required location.
# Base configuration files come from the
# configs/BASE/ directory, see
# configs/BASE/README.txt for an overview of the provides resources.
# Store your additonal resources to
# configs/$TestName.
# It is also possible to overwrite resources from configs/BASE.
# If you define a full config file (instead of config files for individual resources),
# only these will be taken.
copy_configs

# increase the maximum concurrent jobs for FD, SD and DIR
#perl -Mscripts::functions -e 'set_maximum_concurrent_jobs("$conf/susan-dir.conf",100)'
#perl -Mscripts::functions -e 'set_maximum_concurrent_jobs("$conf/susan-sd.conf",100)'
#perl -Mscripts::functions -e 'set_maximum_concurrent_jobs("$conf/susan-fd.conf",100)'


# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


# start the test
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
messages
label storage=File1 pool=Default volume=TestVolume001
@#setdebug level=100 storage=File
@##############################################
@# AllowDuplicates = Yes, both jobs should work
@##############################################
run comment="Should work" level=Full job=AllowDuplicateYes yes
@sleep 2
run comment="Should work" level=Full job=AllowDuplicateYes yes
wait
messages
@$out $tmp/log3.out
@###############################################################
@# Run two jobs with the same level and see wich one is canceled
@###############################################################
run comment="Should work" level=Full job=CancelLowerLevelDuplicatesYes yes
@sleep 2
run comment="Should fail" level=Full job=CancelLowerLevelDuplicatesYes yes
wait
messages
@####################################################################
@# Run two jobs with the different level and see wich one is canceled
@####################################################################
run comment="Should fail" level=Incremental job=CancelLowerLevelDuplicatesYes yes
@sleep 2
run comment="Should work" level=Full job=CancelLowerLevelDuplicatesYes yes
wait
messages
run comment="Should fail" level=Differential job=CancelLowerLevelDuplicatesYes yes
@sleep 2
run comment="Should work" level=Full job=CancelLowerLevelDuplicatesYes yes
wait
messages
run comment="Should work" level=Differential job=CancelLowerLevelDuplicatesYes yes
@sleep 2
run comment="Should fail" level=Incremental job=CancelLowerLevelDuplicatesYes yes
wait
messages
@#####################################################################################
@# Run two jobs with the different level and see wich one is canceled (reversed order)
@#####################################################################################
run comment="Should work" level=Full job=CancelLowerLevelDuplicatesYes yes
@sleep 2
run comment="Should fail" level=Incremental job=CancelLowerLevelDuplicatesYes yes
wait
messages
@$out $tmp/log4.out
@####################################################################
@# Run two jobs, the second one can't cancel the 1st, and should fail
@####################################################################
run comment="Should work" level=Full job=CancelQueueDuplicatesYes yes
@sleep 2
run comment="Should fail" level=Full job=CancelQueueDuplicatesYes yes
wait
messages
@#################################################################
@# The first job should stay queued, the second one will cancel it
@#################################################################
run comment="Should fail" level=Full job=CancelQueueDuplicatesYes when="$when" yes
@sleep 2
run comment="Should work" level=Full job=CancelQueueDuplicatesYes yes
wait
messages
@$out $tmp/log5.out
@########################################
@# The second job will kill the first one
@########################################
run comment="Should fail" level=Full job=CancelRunningDuplicatesYes yes
@sleep 2
run comment="Should work" level=Full job=CancelRunningDuplicatesYes yes
wait
messages
@$out $tmp/log6.out
@##########################
@# The second job won't run
@##########################
run comment="Should work" level=Full job=CancelRunningDuplicatesNo yes
@sleep 1
run comment="Should fail" level=Full job=CancelRunningDuplicatesNo yes
wait
messages
@$out $tmp/log7.out
sql
SELECT JobId, Name, Level, Comment, JobStatus from Job
WHERE Comment='Should fail' and JobStatus='T' ORDER By JobId;

@$out $tmp/log8.out
sql
SELECT JobId, Name, Level, Comment, JobStatus from Job
WHERE Comment='Should work' and JobStatus != 'T' ORDER By JobId;

@$out $tmp/log9.out
sql
SELECT JobId, Name, Level, Comment, JobStatus from Job order by JobId;

quit
END_OF_DATA

run_susan

# verify that all are terminated
check_for_zombie_jobs storage=File1 client=${Client}

# stop susan
stop_susan

touch $tmp/log2.out
check_two_logs

grep '^| *[0-9]' $tmp/log7.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Found errors in $tmp/log7.out"
    print_debug "`cat $tmp/log7.out`"
    estat=1
fi

grep '^| *[0-9]' $tmp/log8.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Found errors in $tmp/log8.out"
    print_debug "`cat $tmp/log8.out`"
    estat=1
fi

nb=`grep '^| *[0-9]' $tmp/log9.out | wc -l`
if [ $nb -lt 10 ]; then
    print_debug "ERROR: no enough results in $tmp/log9.out"
    print_debug "`cat $tmp/log9.out`"
    estat=1
fi

# end tests and check for error codes
end_test

